import tkinter as tk


class DialogBox:
    def __init__(self, position, size, callback):
        self.position = position
        self.size = size
        self.input_text = ""
        self.is_active = False
        self.is_talking = False
        self.root = None
        self.label = None
        self.text = None
        self.scrollbar = None
        self.button = None
        self.callback = callback
        self.name = None

    def get_input(self):
        self.input_text = self.text.get("1.0", tk.END).strip()
        self.callback(self.input_text)
        self.deactivate()

    def activate(self, name=''):
        self.root = tk.Tk()
        self.root.title("对话框")
        self.root.geometry(f"{self.size[0]}x{self.size[1]}")
        # 设置关闭事件的回调函数
        self.root.protocol("WM_DELETE_WINDOW", self.deactivate)
        if name == '':
            self.label = tk.Label(self.root, text=f"回复{self.name}:", font=("Arial", 14))
        else:
            self.label = tk.Label(self.root, text=f"回复{name}:", font=("Arial", 14))
        self.label.pack(pady=10)

        self.text = tk.Text(self.root, font=("Arial", 12), height=10)
        self.text.pack(pady=10)

        self.scrollbar = tk.Scrollbar(self.root)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        self.text.config(yscrollcommand=self.scrollbar.set)
        self.scrollbar.config(command=self.text.yview)

        self.button = tk.Button(self.root, text="发送", font=("Arial", 14), command=self.get_input)
        self.button.pack(pady=10)

    def dialog_up(self, name):
        self.is_talking = True
        self.name = name

    def deactivate(self):
        self.root.destroy()
        self.root = None

    def handle_event(self, event):
        pass

    def draw(self):

        if self.root is not None:
            self.root.mainloop()
        if self.is_talking:
            self.activate()
            self.is_talking = False


def just_test(cc):
    print(cc)


if __name__ == '__main__':
    dialog_box = DialogBox(position=(0, 0), size=(400, 300), callback=just_test)
    dialog_box.dialog_up("杨幂")
    dialog_box.activate()
    print("对话内容：", dialog_box.input_text)
